/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package edu.intec.matsab.dao;

import edu.intec.matsab.persistence.PersistenceManager;
import java.util.logging.Level;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import org.apache.log4j.Logger;

/**
 *
 * @author matilde
 */
public class BasicoDAO {
    
    /** UserAccount bean logger instance */
    private static Logger logger = Logger.getLogger(BasicoDAO.class);



/*
 *
 *
 *
 * */
    public static boolean insertar(Object obj){
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean resultado = false;

        try {
            manager.getTransaction().begin();
            manager.persist(obj);
            manager.getTransaction().commit();

            resultado = true;
            
        } catch (PersistenceException exception) {
                java.util.logging.Logger.getLogger(BasicoDAO.class.getName()).log(Level.SEVERE, null, exception);
        } finally {
            manager.close();
        }
        
        return resultado;
    }

    public static boolean eliminar(Object obj){
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean resultado = false;

        try {
            manager.getTransaction().begin();
            manager.remove(obj);
            manager.getTransaction().commit();

            resultado = true;
        
        } catch (PersistenceException exception) {
            java.util.logging.Logger.getLogger(BasicoDAO.class.getName()).log(Level.SEVERE, null, exception);
        } finally {
            manager.close();
        }
        
        return resultado;
    }    
    
    public static boolean modificar(Object obj){
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager manager = emf.createEntityManager();

        boolean resultado = false;

        try {
            manager.getTransaction().begin();
            manager.merge(obj);
            manager.getTransaction().commit();

            resultado = true;
        
        } catch (PersistenceException exception) {
            java.util.logging.Logger.getLogger(BasicoDAO.class.getName()).log(Level.SEVERE, null, exception);
        } finally {
            manager.close();
        }
        
        return resultado;
    }
    
}
